﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Data;
using e77.Ud2.HeadEnums;
//using e77.MeasureBase;
//using e77.MeasureBase.Helpers;

namespace e77.Ud2
{
    
    public enum EPad
    {
        Home = -2,
        RefPad,
        StripDet,
        P01,
        P02,
        P03,
        P04,
        P05,
        P06,
        P07,
        P08,
        P09,
        P10,
        P11,
        P12
    }

    public class Strip
    {

        // tartalmazza, hogy melyik pad-et milyen LED-ekkel kell megvilágítani
        public Dictionary<EPadNames, ELedMask> eledDict = new Dictionary<EPadNames, ELedMask>
        {
            {EPadNames.Bil, ELedMask.green | ELedMask.cyan | ELedMask.orange| ELedMask.red },
            {EPadNames.Ubg, ELedMask.green | ELedMask.cyan | ELedMask.orange| ELedMask.red },
            {EPadNames.Ket, ELedMask.green | ELedMask.cyan | ELedMask.orange| ELedMask.red},
            {EPadNames.Asc, ELedMask.green | ELedMask.cyan | ELedMask.orange| ELedMask.red },
            {EPadNames.Glu, ELedMask.green | ELedMask.cyan | ELedMask.orange| ELedMask.red },
            {EPadNames.Pro, ELedMask.green | ELedMask.cyan | ELedMask.orange| ELedMask.red },
            {EPadNames.Ery, ELedMask.green | ELedMask.cyan | ELedMask.orange| ELedMask.red },
            {EPadNames.pH,  ELedMask.green | ELedMask.cyan | ELedMask.orange| ELedMask.red },
            {EPadNames.Nit, ELedMask.green },
            {EPadNames.Leu, ELedMask.green | ELedMask.cyan | ELedMask.orange| ELedMask.red },
            {EPadNames.SG,  ELedMask.green | ELedMask.cyan | ELedMask.orange| ELedMask.red },

        };
  

        public enum EPadNames
        {
            Bil = 1,
            Ubg,
            Ket,
            Asc,
            Glu,
            Pro,
            Ery,
            pH,
            Nit,
            Leu,
            Comp,
            SG,

        }
             

        #region Pad Positions

        virtual public int Start_Position
        {
            get
            {
                return 500;  //TODO_HL: Norbi programjában ez az érték 498, miért?
            }
        }

       
        //--------------public lett!
        virtual public int Step_In_Position
        {
            get { return 142; }
        }

        protected Dictionary<EPad, string> _padNames;

        public Dictionary<EPad, string> PadNames
        {
            get
            {
                if (_padNames == null)
                    InitializePadNames();
                return _padNames;
            }
        }

        protected virtual void InitializePadNames()
        {
            _padNames = new Dictionary<EPad, string>();
            Array array = Enum.GetValues(typeof(EPadNames));
            //Array.Find(array, (x) => 
            foreach (EPad pad in ListOfMeasuredStripPad)
            {
                _padNames.Add(pad, Enum.GetValues(typeof(EPadNames))
                .Cast<EPadNames>().Where((x) => (int)x == (int)pad).First().ToString());
            }
        }

        protected Dictionary<EPad, int> _padPositions;

        public Dictionary<EPad, int> PadPositions
        {
            get
            {
                if (_padPositions == null)
                    InitializePadPositions();

                return _padPositions;
            }
        }


        protected virtual void InitializePadPositions()
        {
            _padPositions = new Dictionary<EPad, int>();
            _padPositions.Add(EPad.Home, 0);
            _padPositions.Add(EPad.RefPad, 74);
            _padPositions.Add(EPad.StripDet, 415);
            foreach (EPad pad in ListOfMeasuredStripPad)
                _padPositions.Add(pad, Start_Position + Step_In_Position * ((int)pad - 1));
        }

        private List<EPad> _listOfMeasuredStripPad;
        public List<EPad> ListOfMeasuredStripPad
        {
            get
            {
                if (_listOfMeasuredStripPad == null)
                {
                    _listOfMeasuredStripPad = new List<EPad>();
                    _listOfMeasuredStripPad.AddRange(Enum.GetValues(typeof(EPad)).Cast<EPad>().Where((e) => e.ToString().StartsWith("P")));
                }
                return _listOfMeasuredStripPad;
            }
        }
        #endregion Pad Positions

        #region Constructor and Instance

        protected static Strip _instance = null;

        public static Strip Instance
        {
            get
            {
                if (_instance == null)
                {
                    Trace.TraceInformation("Creating e77.Ud2.Strip");
                    _instance = new Strip();
                }
                return _instance;
            }
        }

        protected void InitInstance()
        {
            if (_instance != null)
            {
                throw new InvalidOperationException("Singelton");
            }
            _instance = this;
        }

        protected Strip()
        {
            InitInstance();
            //InitializeListOfPad();
        }

        #endregion Constructor and Instance

        #region Methods

        /// <summary>
        /// can throw NotFoundException
        /// </summary>
        /// <param name="position_in"></param>
        /// <exception cref="NotFoundException">can throw NotFoundException if the specified int value is
        /// not belongs to any pad position</exception>
        /// <returns>EPad where position is position_in</returns>
        /// 


// ------------------------------------DEV



        //public EPad Position2Pad(int position_in)
        //{
        //    foreach (KeyValuePair<EPad, int> pair in PadPositions)
        //    {
        //        if (pair.Value == position_in)
        //            return pair.Key;
        //    }    
        //        //throw new NotFoundException(string.Format("Cannot found pad for position {0}", position_in));
        //}

// -----------------------------DEV

        #endregion Methods
    }
}